package com.nameless.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nameless.dao.MaintainShopDao;
import com.nameless.dao.OrderDao;
import com.nameless.dao.ShopDao;
import com.nameless.entity.*;
import com.nameless.feign.CarFeign;
import com.nameless.feign.ClientFeign;
import com.nameless.service.OrderService;
import com.nameless.util.JwtUtil;
import com.nameless.util.OrderNum;
import com.nameless.util.WebUtils;
import com.nameless.vo.OrderVo;
import com.nameless.vo.Result;
import org.apache.commons.io.output.CloseShieldOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author keluoyisi
 * @program: car-parent
 * @description:
 * @date 2023-02-28 18:49:44
 */
@Service
@SuppressWarnings("all")
public class OrderServiceImpl extends ServiceImpl<OrderDao, Order> implements OrderService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private CarFeign carFeign;

    @Autowired
    private ClientFeign clientFeign;

    @Autowired
    private ShopDao shopDao;

    @Override
    public Result queryAllByParam(OrderVo orderVo) {
        PageHelper.startPage(orderVo.getPageNo(),orderVo.getPageSize());
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if (orderVo.getOrderNo()!=null){
            wrapper.like("orderNo",orderVo.getOrderNo());
        }
        List<Order> orders = orderDao.selectList(wrapper);
        for (Order order : orders) {
            if (order.getCarId()!=null){
                Car car = carFeign.selectId(order.getCarId());
                if (car!=null){
                    order.setCartName(car.getCarName());
                }

            }
           if (order.getClientId()!=null){
               Client client = clientFeign.selectId(order.getClientId());
               if (client!=null){
                   order.setClientName(client.getName());
               }

           }

            if (order.getBrrowShop()!=null){
                Shop shop = shopDao.selectById(order.getBrrowShop());
                if (shop!=null){
                    order.setShopName(shop.getName());
                }
            }

            if (order.getReturnShop()!=null){
                Shop shop = shopDao.selectById(order.getReturnShop());
                if (shop!=null){
                    order.setReturnName(shop.getName());
                }
            }
        }
        PageInfo<Order> info = new PageInfo<>(orders);
        return new Result(200,"成功",info);
    }

    @Override
    public Result deleteById(Integer id) {
        int i = orderDao.deleteById(id);
        return new Result(200,"成功");
    }

    @Override
    public Result updateOrder(Order order) {
        int i = orderDao.updateById(order);
        return new Result(200,"成功");
    }
    @Override
    public Result updateOrder1(Order order) {
        int i = orderDao.updateById(order);
        return new Result(200,"成功");
    }

    @Override
    public Result insertOrder(String startTime, String endTime, Car car) {
        System.out.println(car);
        Date date =null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("date==="+date);
        String format_DateTime = "yyyy-MM-dd HH:mm:ss";
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format_DateTime);
        LocalDateTime localDateTime = LocalDateTime.parse(endTime, df);
        System.out.println("localDateTime==="+localDateTime);

        Order order = new Order();
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> map = JwtUtil.getTokenChaim(token);
        String clientname = (String) map.get("clientname");
        System.out.println(clientname);
        Client client = clientFeign.selectName(clientname);
        System.out.println(client);
        Long i = Long.valueOf(new Random().nextInt(100));
        String orderNum = OrderNum.getOrderNum(i);
        order.setClientId(client.getId());
        order.setCarId(car.getId());
        order.setStartTime(date);
        order.setEndTime(localDateTime);
        order.setBrrowShop(car.getShopId());
        order.setReturnShop(car.getShopId());
        order.setTotalPrice(car.getPrice());
        order.setPreparidAmount(car.getPrice());
        order.setState(1);
        order.setAddtime(LocalDateTime.now());
        order.setOrderNo(orderNum);
        int insert = orderDao.insert(order);
        return new Result(200,"成功",order);
    }

    @Override
    public Result selectOrder(Order order) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("orderNo", order.getOrderNo());
        Order selectOne = orderDao.selectOne(wrapper);
        Integer state = selectOne.getState();
        if (state==4){
            return new Result(200,"支付成功");
        }
        return new Result(2001,"支付失败");
    }


}
